package com.lyncs.ods.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.google.common.collect.Maps;
import com.lyncs.ods.common.exception.ApiException;
import com.lyncs.ods.constant.LyncsOdsConstant;
import com.lyncs.ods.properties.WxMpProperties;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.service.WxService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.parameters.P;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * WeChat open config
 *
 * @author alex
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(WxMpProperties.class)
public class WxMpConfiguration {
    private static Map<String, Object> mpServices = Maps.newHashMap();

    public static <T> T getMpService(String appId, Class<T> clazz) {
        Object object = mpServices.get(appId);
        if (Objects.isNull(object)) {
            log.error("appid:{} 配置信息不存在", appId);
            throw new ApiException("未找到应用的配置信息");
        }
        return (T) object;
    }

    @Autowired
    private WxMpProperties properties;

    @PostConstruct
    public void initServices() {
        // 代码里 getConfigs()处报错的同学，请注意仔细阅读项目说明，你的IDE需要引入lombok插件！！！！
        final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs();
        if (configs == null) {
            throw new ApiException("第三方账号信息未配置");
        }

        configs.forEach(config -> {
            Object wxService = getWxService(config);
            if (Objects.nonNull(wxService)) {
                mpServices.put(config.getAppId(), wxService);
            }
        });
    }

    private Object getWxService(WxMpProperties.MpConfig config) {
        if (LyncsOdsConstant.AuthPlatform.WECHAT_WEB_APP.getKey().equals(config.getAesKey())) {
            WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
            wxMpDefaultConfig.setAppId(config.getAppId());
            wxMpDefaultConfig.setSecret(config.getSecret());
            wxMpDefaultConfig.setToken(config.getToken());
            wxMpDefaultConfig.setAesKey(config.getAesKey());
            WxMpService service = new WxMpServiceImpl();
            service.setWxMpConfigStorage(wxMpDefaultConfig);
            return service;
        } else if (LyncsOdsConstant.AuthPlatform.WECHAT_MINI_APP.getKey().equals(config.getAesKey())) {
            WxMaService service = new WxMaServiceImpl();
            WxMaDefaultConfigImpl wxMaDefaultConfig = new WxMaDefaultConfigImpl();
            wxMaDefaultConfig.setAppid(config.getAppId());
            wxMaDefaultConfig.setSecret(config.getSecret());
            wxMaDefaultConfig.setToken(config.getToken());
            wxMaDefaultConfig.setAesKey(config.getAesKey());
            service.setWxMaConfig(wxMaDefaultConfig);
            return service;
        } else {
            log.error("do not support platform:{}", config.getAesKey());
            return null;
        }
    }
}